package com.Service.DAO;


import com.Entity.AccountEntity;
import com.Service.impl.AccountImpl;
import org.hibernate.Session;
import org.hibernate.Transaction;
import org.hibernate.query.Query;


import java.util.List;

public class AccountDAO implements AccountImpl {
    private Session session = null;
    private Transaction ts = null;
    private String sql = "";

    @Override
    public List Login(AccountEntity user) {
        try {
            getSession();
            sql = "select id from AccountEntity where id=? and password=? ";
            Query query = session.createQuery(sql);
            query.setParameter(0, user.getId());
            query.setParameter(1, user.getPassword());
            List list = query.list();
            AccountEntity account = session.find(AccountEntity.class, user.getId());
            ts.commit();
            if (!list.isEmpty())
                return list;
            else
                return null;
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        } finally {
            session.close();
        }
    }

    @Override
    public boolean Register(AccountEntity user) {

        // TODO Auto-generated method stub
        try {
            getSession();
            session.save(user);
            ts.commit();
            return true;
        } catch (Exception ex) {
            ex.printStackTrace();
            return false;
        } finally {
            session.close();
        }
    }

    @Override
    public List Search(String name) {
        try {
            getSession();

            String hql = "from AccountEntity as s where s.name like ?";
            Query query = session.createQuery(hql);
            query.setParameter(0,"%"+name+"%");
            List list = query.list();
            ts.commit();
            if (!list.isEmpty())
                return list;
            else
                return null;
        } catch (Exception ex) {
            ex.printStackTrace();
            return null;
        } finally {
            session.close();
        }
    }

    @Override
    public AccountEntity get(AccountEntity entity) {
        AccountEntity account = null;
        try {
            getSession();
            account = session.find(AccountEntity.class, entity.getId());
            ts.commit();
        } catch (Exception e) {
            if (ts != null) {
                ts.rollback();
            }
            account = null;
            e.printStackTrace();
            System.out.println("public Employee get(Employee entity) error");
        }finally {
            if (session != null) {
                session.close();
            }
        }
        return account;
    }

    @Override
    public List<AccountEntity> getAll() {
        List<AccountEntity> accounts = null;
        try {
            getSession();
            accounts = session.createQuery("FROM AccountEntity").list();
            ts.commit();
        } catch (Exception e) {
            if (ts != null) {
                ts.rollback();
            }
            accounts = null;
            e.printStackTrace();
            System.out.println("public List<AccountEntity> getAll error");

        }finally {
            if (session != null) {
                session.close();
            }
        }
        return accounts;
    }

    @Override
    public boolean update(AccountEntity entity) {
        boolean result = false;
        try {
           getSession();
           session.saveOrUpdate(entity);
           ts.commit();
           result = true;
        } catch (Exception e) {
            if (ts != null) {
                ts.rollback();
            }
            result = false;
            e.printStackTrace();
            System.out.println("public boolean update(AccountEntity entity) error");
        }finally {
            if (session != null) {
                session.close();
            }
        }
        return result;
    }

    @Override
    public boolean delete(AccountEntity entity) {
        boolean result = false;
        try {
            getSession();
            session.delete(entity);
            ts.commit();
            result = true;
        } catch (Exception e) {
            if (ts != null) {
                ts.rollback();
            }
            result = false;
            e.printStackTrace();
            System.out.println("public boolean delete(AccountEntity entity) error");
        }finally {
            if (session != null) {
                session.close();
            }
        }
        return result;
    }


    public void getSession() {
        session = com.Util.Hibernateutil.getSessionFactory().openSession();
        this.ts = session.beginTransaction();
    }
}
